package main

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"net/http"
	"regexp"
	"sync"
)

type Url string

// 请求符合要求的Url得到resp.Body

type UrlDealer struct {
	client *http.Client
	rules  []*regexp.Regexp
	group  *sync.WaitGroup
}

type UrlDealerManager struct {
	Manager
}

func NewUrlDealerManager() *UrlDealerManager {
	return &UrlDealerManager{}
}

func NewUrlDealer(group *sync.WaitGroup) *UrlDealer {
	return &UrlDealer{
		client: &http.Client{},
		group:  group,
	}
}

func (dealer *UrlDealer) Run(ctx context.Context) {
	dealer.group.Add(1)
	for {
		select {
		case <-ctx.Done():
			defer dealer.group.Done()
			return
		case url := <-UrlContainer:
			err := dealer.Request(string(url), "")
			if err != nil {
				fmt.Println("request Err,", err)
			}
		}
	}
}

func (c *UrlDealer) Request(url string, req_Body string) (error error) {
	if matchUrl(url, c.rules) {
		fmt.Println("doRequest url,", url)
		request, err := http.NewRequest("Get", url, bytes.NewBuffer([]byte(req_Body)))
		if err != nil {
			error = fmt.Errorf("ERROR!Generate request err,%s", err)
			return
		}
		resp, err := c.client.Do(request)
		if err != nil {
			error = fmt.Errorf("ERROR!get resp err,%s", err)
			return
		}
		body, err := io.ReadAll(resp.Body)
		if err != nil {
			error = fmt.Errorf("ERROR!Read resp_body err,%s", err)
			return
		}
		PageContainer <- NewPage(url, body)
		return
	}

	error = fmt.Errorf("url not match rules")
	return
}

func matchUrl(url string, rules []*regexp.Regexp) bool {
	if len(rules) == 0 {
		return true
	}
	for _, exp := range rules {
		if exp.MatchString(url) {
			return true
		}
	}
	return false
}
